// 引入字体文件
require('../../fonts/iconfont.css')

/**
 * 工具函数
 */

;
(function () {
    /**
     * 轻提示
     */
    function toast(type, text) {
        const div = document.createElement('div')
        document.body.append(div)
        div.className = 'toast'
        div.innerHTML = `
            <i class='iconfont icon-${type}'></i>
            <span>${text}</span>
        `
        setTimeout(() => div.remove(), 1500)
    }


    /**
     * 显示导航
     */
    function showNav(select) {
        const div = document.createElement('div')
        document.body.append(div)
        div.className = 'home-nav dpflex'
        div.innerHTML = `
            <a href="./home.html" class="${select === 'home' ? 'active':''}">
                <i class="iconfont icon-home"></i>
                <span>首页</span>
            </a>
            <a href="./run.html" class="${select === 'sport' ? 'active':''}">
                <i class="iconfont icon-sport"></i>
                <span>运动</span>
            </a>
            <a href="./myhome.html" class="${select === 'myhome' ? 'active':''}">
                <i class="iconfont icon-mine"></i>
                <span>我的</span>
            </a>
        `
    }

    /**
     * 运动页面小标签
     */
    function sprotNav(select) {
        const div = document.createElement('div')
        document.body.append(div)
        div.className = 'sport-title dpflex justspacebt pb10 pt20'
        div.innerHTML = `
        <a class="flex1 ${select == 'run' ? 'active' : ''}" href="./run.html">跑步</a>
        <a class="flex1 ${select == 'riding' ? 'active' : ''}" href="./riding.html">骑行</a>
        <a class="flex1 ${select == 'training-course' ? 'active' : ''}" href="./training-course.html">课程训练</a>
        `
    }

    /**
     * 补零
     */
    function zero(num) {
        return num >= 10 ? num : '0' + num
    }

    /**
     * 判断用户是否登录
     */
    function isLogin() {
        const userId = localStorage.getItem('userId')
        if (!userId) location.href = './login.html'
    }

    /**
     * 获取时间
     */
    function getTimer(normalTimer) {
        let h = Math.floor(normalTimer / 60 / 60);
        let m = Math.floor(normalTimer / 60) % 60;
        let s = Math.floor(normalTimer % 60);
        return [h, m, s].map(item => {
            return zero(item)
        }).join(':')
    }

    /* 
    根据经纬度计算距离
        startPoint:  第一个点
        curPoint:  第二个点
    */
    function calcDistance(startPoint, curPoint) {
        let lat1 = startPoint.lat
        let lng1 = startPoint.lng

        let lat2 = curPoint.lat
        let lng2 = curPoint.lng

        var radLat1 = lat1 * Math.PI / 180.0;
        var radLat2 = lat2 * Math.PI / 180.0;
        var a = radLat1 - radLat2;
        var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137;
        s = Math.round(s * 10000) / 10000;
        return s.toFixed(2) // 单位千米
    }

    /* 计算配速函数 
      d： 距离
      s： 时间
    */
    function calcPace(d, s) {
        let distance = d
        let spendTime = s / 60
        if (distance > 0 && spendTime > 0) {
            let pace = parseFloat(spendTime / distance)
            let m = Math.floor(pace)
            let s = ((pace - m) * 60).toFixed(0)
            return `${m}'${s}"`
        }
        return "--"
    }

    /* 计算卡路里 */
    function calCalorie(miles) {
        if (miles > 0) {
            let calorie = 60 * miles * 1.036;
            return calorie.toFixed(2)
        }
        return "--"
    }

    /**
     * 添加点
     */
    function addMarker(amap, point, pic) { // 创建图标对象   
        // 创建图标对象   
        const myIcon = new BMap.Icon(pic, new BMap.Size(45, 45), { //图标尺寸    
            anchor: new BMap.Size(15, 30), // 设置偏移量
            imageSize: new BMap.Size(30, 32) // 设置图片大小  
        });
        // 创建标注对象并添加到地图   
        let marker = new BMap.Marker(point, {
            icon: myIcon
        });
        //将标注对象添加到某个具体的地图上
        amap.addOverlay(marker);
    }

    // 添加折现
    function addpolyline(amap, startpoint, endpoint) {
        let polyline = new BMap.Polyline([
            new BMap.Point(startpoint.lng, startpoint.lat),
            new BMap.Point(endpoint.lng, endpoint.lat)
        ], {
            strokeColor: "blue",
            strokeWeight: 6,
            strokeOpacity: 0.5
        });
        amap.addOverlay(polyline);
    }


    module.exports = {
        toast,
        showNav,
        zero,
        isLogin,
        sprotNav,
        getTimer,
        calcDistance,
        calcPace,
        calCalorie,
        addMarker,
        addpolyline
    }
})();